<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>封装矩形绘制</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      canvas {
        display: block;
        width: 100vw;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script src="./main/index.js"></script>
    <script>
      const shape = new Shape("#canvas");
      // shape.rect(0.5, 0.5, 0.2, 0.2);
      let point = 0;
      let arr = [];
      window.addEventListener("click", (event) => {
        const x = (event.clientX / window.innerWidth - 0.5) * 2; // 0~1 -0.5~0.5 -1~1
        const y = -(event.clientY / window.innerHeight - 0.5) * 2;
        // shape.drawPoint(x, y);
        point++;
        arr.push([x, y]);
        shape.drawPoint(x, y);
        if (point == 2) {
          shape.drawPoint(arr[0][0], arr[0][1]);
          const width = arr[1][0] - arr[0][0];
          const height = arr[1][1] - arr[0][1];
          shape.rect(arr[0][0], arr[0][1], width, height);
          arr = [];
          point = 0;
        }
      });
    </script>
  </body>
</html>
